package com.study.spring_ai;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.metadata.ChatGenerationMetadata;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.template.st.StTemplateRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.Map;

@Service
public class AIService {


    private ChatClient client;

    public AIService(OllamaChatModel ollamaChatModel){
    this.client = ChatClient.builder(ollamaChatModel)
                .defaultTemplateRenderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()).build();
    }


    @Autowired
    private OllamaChatModel ollamaChatModel;

    public String generate(String prompt) {
        client.prompt().templateRenderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build());
        return client.prompt().user(prompt).call()
                //结构化输出方式
                .entity(new ParameterizedTypeReference<>(){});
    }

    /**
     *  响应式
     * @param prompt
     * @return
     */
    public Flux<String> generate2(String prompt) {
        return client.prompt().user(prompt).stream().content();
    }
}